#!/usr/bin/env python3
import logging
import logging.handlers
import sys


class myLogger(object):
    def __init__(self, name='demo', file='', level=logging.INFO, filesize=2*1024*1024, filecount=20) -> None:
        self.logger = logging.getLogger(name)
        self.logger.setLevel(level)

        self.shandle = logging.StreamHandler(sys.stderr)
        self.shandle.setLevel(level)
        self.shandle.setFormatter(logging.Formatter(f"%(asctime)s - %(levelname)s: %(message)s{' (%(name)s)' if name else ''}"))
        self.logger.addHandler(self.shandle)

        if file:
            self.fhandle = logging.handlers.RotatingFileHandler(file, maxBytes=filesize, backupCount=filecount)
            self.fhandle.setLevel(level)
            self.fhandle.setFormatter(logging.Formatter(f"%(asctime)s - %(levelname)s: %(message)s{' (%(name)s)' if name else ''}"))
            self.logger.addHandler(self.fhandle)

    def debug(self, msg, *args, **kwargs):
        self.logger.debug(msg, *args, **kwargs)

    def info(self, msg, *args, **kwargs):
        self.logger.info(msg, *args, **kwargs)

    def warning(self, msg, *args, **kwargs):
        self.logger.warning(msg, *args, **kwargs)

    def error(self, msg, *args, **kwargs):
        self.logger.error(msg, *args, **kwargs)
